#include <sdk.h>
#include "occurrenceshighlightingconfigurationpanel.h"
#include "M_Code_colourmanager.h"

BEGIN_EVENT_TABLE( OccurrencesHighlightingConfigurationPanel, cbConfigurationPanel )
  EVT_BUTTON( XRCID( "btnHighlightColour" ), OccurrencesHighlightingConfigurationPanel::OnChooseColour )
  EVT_BUTTON( XRCID( "btnHighlightPermanentlyColour" ), OccurrencesHighlightingConfigurationPanel::OnChooseColour )
  EVT_CHECKBOX( XRCID( "chkHighlightOccurrences" ), OccurrencesHighlightingConfigurationPanel::OnCheckHighlightOccurrences )
END_EVENT_TABLE()

OccurrencesHighlightingConfigurationPanel::OccurrencesHighlightingConfigurationPanel( wxWindow* parent ) {
  ConfigManager* cfg = Manager::Get()->GetConfigManager( _T( "editor" ) );
  if( !cfg ) {
    return;
  }
  wxXmlResource* xml_res = wxXmlResource::Get();
  wxString resPath = ConfigManager::GetDataFolder() + _T( "/occurrenceshighlightingconfigurationpanel.xrc" );
  xml_res->Load( resPath );
  if( !xml_res->LoadObject( this, parent, _T( "OccurrencesHighlightingConfigurationPanel" ), _T( "wxPanel" ) ) ) {
    return;
  }
  bool highlightEnabled = cfg->ReadBool( _T( "/highlight_occurrence/enabled" ), true );
  XRCCTRL( *this, "chkHighlightOccurrences",              wxCheckBox )->SetValue( highlightEnabled );
  XRCCTRL( *this, "chkHighlightOccurrencesCaseSensitive", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/highlight_occurrence/case_sensitive" ), true ) );
  XRCCTRL( *this, "chkHighlightOccurrencesCaseSensitive", wxCheckBox )->Enable( highlightEnabled );
  XRCCTRL( *this, "chkHighlightOccurrencesWholeWord",     wxCheckBox )->SetValue( cfg->ReadBool( _T( "/highlight_occurrence/whole_word" ), true ) );
  XRCCTRL( *this, "chkHighlightOccurrencesWholeWord",     wxCheckBox )->Enable( highlightEnabled );
  wxColour highlightColour = Manager::Get()->GetColourManager()->GetColour( _T( "editor_highlight_occurrence" ) );
  XRCCTRL( *this, "btnHighlightColour", wxButton )->SetBackgroundColour( highlightColour );
  XRCCTRL( *this, "stHighlightColour",  wxStaticText )->Enable( highlightEnabled );
  XRCCTRL( *this, "btnHighlightColour", wxButton )->Enable( highlightEnabled );
  wxSpinCtrl *minLength = XRCCTRL( *this, "spnHighlightLength", wxSpinCtrl );
  minLength->SetValue( cfg->ReadInt( _T( "/highlight_occurrence/min_length" ), 3 ) );
  minLength->Enable( highlightEnabled );
  XRCCTRL( *this, "stHighlightLength", wxStaticText )->Enable( highlightEnabled );
  XRCCTRL( *this, "chkHighlightOccurrencesPermanentlyCaseSensitive", wxCheckBox )->SetValue( cfg->ReadBool( _T( "/highlight_occurrence/case_sensitive_permanently" ), true ) );
  XRCCTRL( *this, "chkHighlightOccurrencesPermanentlyWholeWord",     wxCheckBox )->SetValue( cfg->ReadBool( _T( "/highlight_occurrence/whole_word_permanently" ), true ) );
  highlightColour = Manager::Get()->GetColourManager()->GetColour( _T( "editor_highlight_occurrence_permanently" ) );
  XRCCTRL( *this, "btnHighlightPermanentlyColour",                   wxButton )->SetBackgroundColour( highlightColour );
}

OccurrencesHighlightingConfigurationPanel::~OccurrencesHighlightingConfigurationPanel() {
}

void OccurrencesHighlightingConfigurationPanel::OnApply() {
  ConfigManager* cfg = Manager::Get()->GetConfigManager( _T( "editor" ) );
  cfg->Write( _T( "/highlight_occurrence/enabled" ),        XRCCTRL( *this, "chkHighlightOccurrences",              wxCheckBox )->GetValue() );
  cfg->Write( _T( "/highlight_occurrence/case_sensitive" ), XRCCTRL( *this, "chkHighlightOccurrencesCaseSensitive", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/highlight_occurrence/whole_word" ),     XRCCTRL( *this, "chkHighlightOccurrencesWholeWord",     wxCheckBox )->GetValue() );
  wxColour highlightColour = XRCCTRL( *this, "btnHighlightColour", wxButton )->GetBackgroundColour();
  Manager::Get()->GetColourManager()->SetColour( _T( "editor_highlight_occurrence" ), highlightColour );
  cfg->Write( _T( "/highlight_occurrence/min_length" ),     XRCCTRL( *this, "spnHighlightLength",                   wxSpinCtrl )->GetValue() );
  cfg->Write( _T( "/highlight_occurrence/case_sensitive_permanently" ), XRCCTRL( *this, "chkHighlightOccurrencesPermanentlyCaseSensitive", wxCheckBox )->GetValue() );
  cfg->Write( _T( "/highlight_occurrence/whole_word_permanently" ),     XRCCTRL( *this, "chkHighlightOccurrencesPermanentlyWholeWord",     wxCheckBox )->GetValue() );
  highlightColour = XRCCTRL( *this, "btnHighlightPermanentlyColour", wxButton )->GetBackgroundColour();
  Manager::Get()->GetColourManager()->SetColour( _T( "editor_highlight_occurrence_permanently" ), highlightColour );
}

void OccurrencesHighlightingConfigurationPanel::OnCancel() {
}

wxString OccurrencesHighlightingConfigurationPanel::GetTitle() const {
  return _T( "Occurrences Highlighting" );
}

wxString OccurrencesHighlightingConfigurationPanel::GetBitmapBaseName() const {
  return _T( "occurrenceshighlighting" );
}

void OccurrencesHighlightingConfigurationPanel::OnChooseColour( wxCommandEvent& event ) {
  wxColourData data;
  wxWindow* sender = FindWindowById( event.GetId() );
  data.SetColour( sender->GetBackgroundColour() );
  wxColourDialog dlg( this, &data );
  PlaceWindow( &dlg );
  if( dlg.ShowModal() == wxID_OK ) {
    wxColour colour = dlg.GetColourData().GetColour();
    sender->SetBackgroundColour( colour );
    sender->SetLabel( wxEmptyString );
  }
}

void OccurrencesHighlightingConfigurationPanel::OnCheckHighlightOccurrences( wxCommandEvent& event ) {
  XRCCTRL( *this, "chkHighlightOccurrencesCaseSensitive", wxCheckBox )->Enable( event.IsChecked() );
  XRCCTRL( *this, "chkHighlightOccurrencesWholeWord",     wxCheckBox )->Enable( event.IsChecked() );
  XRCCTRL( *this, "stHighlightColour",                    wxStaticText )->Enable( event.IsChecked() );
  XRCCTRL( *this, "btnHighlightColour",                   wxButton )->Enable( event.IsChecked() );
  XRCCTRL( *this, "spnHighlightLength",                   wxSpinCtrl )->Enable( event.IsChecked() );
}

